clear all;
clc;

load mnist_train;

ic = [2,3];
nic = size(ic,1);

C = 1000;

Y = yL';
Xt = xL';
clear xL;
clear yL;

l = size(Y,1);
[n,d] = size(Xt);

cls = unique(Y);
c = size(cls,1);

sz = zeros(c,1);
idx = cell(c,1);
for i = 1:c
    idx{i}=find(Y==cls(i));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Y = cell(c,c);

w = zeros(nic,d);
b = zeros(nic,1);

for k = 1:nic
    i = ic(k,1)+1;
    j = ic(k,2)+1;
    disp(['Training pair ',int2str(i-1),' vs ',int2str(j-1)]);
    ix = [idx{i};idx{j}];
    X = Xt(ix,:);
    Y = [ones(sz(i),1);-ones(sz(j),1)];
    [l,d] = size(X);
    tic
    cvx_begin
    %cvx_solver sedumi
    variable w(d,1)
    variable b
    variable del(l,1) nonnegative
    minimize (w'*w+2*C*sum(del));
    Y.*(X*w+b)+del-ones(l,1) >= 0;
    cvx_end
    toc
end